package com.hqyj.web.servlet;

import com.hqyj.pojo.User;
import com.hqyj.service.UserService;
import com.hqyj.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author cz
 * 2021/8/12 18:51
 */
@WebServlet("/modifyPassword.do")
public class ModifyPasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        UserService userService = new UserServiceImpl();
        String username = req.getParameter("name");
        String pass = req.getParameter("pass");
        String newpass = req.getParameter("newpass");
        User user = userService.findUser(username,pass);
        if (user != null){
            userService.modifyPassword(user,newpass);
            resp.getWriter().print("<script>alert('修改成功')</script>");
            resp.setHeader("Refresh","0;url='/index.jsp'");
        }else {
            req.setAttribute("msg","原密码错误。。。");
            req.getRequestDispatcher("/updatepassword.jsp").forward(req,resp);
        }
    }
}
